package jmh;

import java.util.ArrayList;
import java.util.concurrent.TimeUnit;

import org.ahocorasick.trie.Trie;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.OutputTimeUnit;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.Setup;
import org.openjdk.jmh.annotations.State;

import ac.BM;
import ac.KMP;

@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@State(Scope.Thread)
public class AppBenchmark {
    private ArrayList<BM> bmArray = new ArrayList<BM>();
    private ArrayList<KMP> kmpArray = new ArrayList<KMP>();
    private Trie acTrie;

    @Setup
    public void setup() {
        var patterns = new ArrayList<String>();
        for (int i = 0; i < 1000; i++) {
            var pattern = String.format("www.%d.com.cn", i);
            patterns.add(pattern);
            var bm = new BM(pattern);
            bmArray.add(bm);
            var kmp = new KMP(pattern);
            kmpArray.add(kmp);
        }
        acTrie = Trie.builder().addKeywords(patterns).build();
    }

    @Benchmark
    public void bmArrayBenchmark() {
        for (var bm : bmArray) {
            if (bm.containsMatch("www.500.com.cn")) {
                return;
            }
        }
    }

    @Benchmark
    public void kmpArrayBenchmark() {
        for (var kmp : kmpArray) {
            if (kmp.containsMatch("www.500.com.cn")) {
                return;
            }
        }
    }

    @Benchmark
    public void acTrieBenchmark() {
        acTrie.containsMatch("www.500.com.cn");
    }
}
